package com.srit.tag.html;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author guan
 * 
 */
public class Html {
	public static final String SPACE = " ";
	public static final String END = "/";
	public static final String LINE = "\r";
	public static final String TAB = "\t";
	private String tagName;// 标签名

	private String id;// ID
	private String name;// name属性
	private String className;// css类属性
	private Map<String, String> attrMap = new HashMap<String, String>();
	private List<Html> sonList = new ArrayList<Html>();
	private String innerHtml;// 标签包含的html
	private Map<String, String> styleMap = new HashMap<String, String>();

	public Html(String tagName) {
		this.setTagName(tagName);
	}

	public String getId() {
		return this.attrMap.get("id");
	}

	public void setId(String id) {
		this.attrMap.put("id", id);
	}

	public String getClassName() {
		return this.attrMap.get("class");
	}

	public void setClassName(String className) {
		this.attrMap.put("class", className);
	}

	public String getName() {
		return this.attrMap.get("name");
	}

	public void setName(String name) {
		this.attrMap.put("name", name);
	}

	public void attr(String id, String value) {
		this.attrMap.put(id, value);
	}

	public void addSon(Html son) {
		this.sonList.add(son);
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getInnerHtml() {
		return innerHtml;
	}

	public void setInnerHtml(String innerHtml) {
		this.innerHtml = innerHtml;
	}

	public void css(String key, String value) {
		this.styleMap.put(key, value);
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("<" + this.getTagName() + SPACE + this.attr() + this.style()
				+ ">" + LINE);
		sb.append(sonListHtml(this.sonList));
		sb.append(this.innerHtml == null ? "" : this.innerHtml + LINE);
		sb.append("<" + END + this.getTagName() + ">" + LINE);
		return sb.toString();
	}

	public static void main(String[] args) {
		Html table = new Html("table");
		table.setId("tableId");
		table.setName("tabelName");
		table.attr("onclick", "test()");

		Html td = new Html("td");
		td.setId("tdId");
		td.setName("tdName");
		td.setClassName("tdClass");
		td.setInnerHtml("hahaha");
		table.addSon(td);

		System.out.println(table);
	}

	private String attr() {
		StringBuffer sb = new StringBuffer();
		for (Iterator<String> it = this.attrMap.keySet().iterator(); it
				.hasNext();) {
			String key = it.next();
			String value = this.attrMap.get(key);
			sb.append(key + "=\"" + value + "\"" + SPACE);
		}
		return sb.toString();
	}

	public String style() {
		StringBuffer sb = new StringBuffer();
		sb.append(" style=\"");
		for (Iterator<String> it = styleMap.keySet().iterator(); it.hasNext();) {
			String key = it.next();
			String value = this.styleMap.get(key);
			sb.append(key + ":" + value + ";");
		}
		sb.append("\"");
		return sb.toString();
	}

	private String sonListHtml(List<Html> sonList) {
		StringBuffer sb = new StringBuffer();
		// sb.append(TAB);
		for (Iterator<Html> it = sonList.iterator(); it.hasNext();) {
			sb.append(it.next());
		}
		// System.out.println(sb.lastIndexOf("<"));
		// int lastIndex = sb.lastIndexOf("<");
		// if (lastIndex != -1) {
		// sb.replace(lastIndex, lastIndex, TAB);
		// }
		return sb.toString();
	}

}
